package cn.consult.sys.service.impl;


import cn.consult.common.utils.GetStringParseDate;
import cn.consult.sys.dao.TApartmentApplyforDao;
import cn.consult.sys.dao.TPlaceBuildingDao;
import cn.consult.sys.dao.TPlaceFloorDao;
import cn.consult.sys.dao.TPlaceRoomDao;
import cn.consult.sys.entity.TApartmentApplyfor;
import cn.consult.sys.entity.TPlaceBuilding;
import cn.consult.sys.entity.TPlaceFloor;
import cn.consult.sys.entity.TPlaceRoom;
import cn.consult.sys.service.TApartmentApplyforService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.Map;

/**
 * 公寓申请审批表(TApartmentApplyfor)表服务实现类
 *
 * @author makejava
 * @since 2024-04-02 09:49:13
 */
@Service("tApartmentApplyforService")
public class TApartmentApplyforServiceImpl extends ServiceImpl<TApartmentApplyforDao, TApartmentApplyfor> implements TApartmentApplyforService {

    @Override
    public IPage<TApartmentApplyfor> getPage(Map<String, Object> params) {
        LambdaQueryWrapper<TApartmentApplyfor> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(params.get("applyforName") != null && params.get("applyforName") != "", TApartmentApplyfor::getApplyforName, params.get("applyforName"));
        wrapper.eq(params.get("applyforStatus") != null && params.get("applyforStatus") != "", TApartmentApplyfor::getApplyforStatus, params.get("applyforStatus"));
        wrapper.eq(params.get("applyforUnit") != null && params.get("applyforUnit") != "", TApartmentApplyfor::getApplyforUnit, params.get("applyforUnit"));

        Page<TApartmentApplyfor> tApartmentApplyforPage = new Page<>();
        tApartmentApplyforPage.setCurrent(Long.valueOf((String) params.get("pageNum")));
        tApartmentApplyforPage.setSize(Long.valueOf((String) params.get("pageSize")));

        Page<TApartmentApplyfor> page = this.baseMapper.selectPage(tApartmentApplyforPage, wrapper);


        return page;
    }

    @Autowired
    private TPlaceBuildingDao tPlaceBuildingDao;
    @Autowired
    private TPlaceRoomDao tPlaceRoomDao;
    @Autowired
    private TPlaceFloorDao tPlaceFloorDao;

    @Override
    public boolean saveExamine(TApartmentApplyfor tApartmentApplyfor) {
        tApartmentApplyfor.setCreateTime(new Date().toString());
        tApartmentApplyfor.setApplyforStatus(String.valueOf(1));
        boolean flag = false;
        try {
            String timeString = GetStringParseDate.stringToDateString(tApartmentApplyfor.getApplyforCheckinTime());
            tApartmentApplyfor.setApplyforCheckinTime(timeString);

            Long bulidingId = tApartmentApplyfor.getBulidingId();
            Long floorId = tApartmentApplyfor.getFloorId();
            Long roomId = tApartmentApplyfor.getRoomId();
            TPlaceBuilding tPlaceBuilding = tPlaceBuildingDao.selectById(bulidingId);
            TPlaceFloor tPlaceFloor = tPlaceFloorDao.selectById(floorId);
            TPlaceRoom room = tPlaceRoomDao.selectById(roomId);

            String buildingName = tPlaceBuilding.getBuildingName();
            String floorName = tPlaceFloor.getFloorSerialnumber();
            String roomSerialnumber = room.getRoomSerialnumber() + "号";

            tApartmentApplyfor.setJournalNumber(room.getRoomSerialnumber());
            tApartmentApplyfor.setApplyforLocation(buildingName + "-" + floorName + "-" + roomSerialnumber);

            if (this.baseMapper.insert(tApartmentApplyfor) > 0) {
                flag = true;
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }


        return flag;
    }
}

